package com.study.d012Components;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.util.ArrayList;
import java.util.List;

import javax.swing.filechooser.FileSystemView;

import com.sun.javafx.tk.FileChooserType;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

/**
*@description 文件夹选项
*@author xtayhame
*date 2020年3月7日
*version
*since v1.0
*/
public class FileChooserMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane= new AnchorPane();
		VBox box = new VBox();
		box.setSpacing(10);
		Button b1 = new Button("打开单个文件");
		Button b2 = new Button("打开多个文件");
		Button b3 = new Button("打开文件夹");
		Button b4 = new Button("保存文件");

		TextArea ta = new TextArea();
		//设置换行
		ta.setWrapText(true);

		box.getChildren().addAll(b1,b2,b3,b4,ta);

		anchorPane.getChildren().add(box);

		stage.setScene(new Scene(anchorPane, 600, 400));
		stage.show();


		//打开单个文件
		b1.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				//获取桌面路径
				File deskFile=FileSystemView.getFileSystemView().getHomeDirectory();

				FileChooser fc = new FileChooser();
				fc.setInitialDirectory(deskFile);

				//设置窗口名称
				fc.setTitle("打开文件");

				//添加过滤
				fc.getExtensionFilters().addAll(
						new ExtensionFilter("文本文件", "*.txt"),
						new ExtensionFilter("Java文件", "*.java")
						);

				Stage stage = new Stage();
				File file= fc.showOpenDialog(stage);
				if(file ==null){
					return;
				}

				//BufferedInputStream bis= new BufferedInputStream(new FileInputStream(file));
				BufferedReader br = null;
				try {
					br = new BufferedReader(new FileReader(file));
					String line=null;
					while((line=br.readLine()) !=null){
						ta.appendText(line);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					if(br !=null){
						try {
							br.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		});


		//打开多个文件
		b2.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				//获取桌面路径
				File deskFile=FileSystemView.getFileSystemView().getHomeDirectory();

				FileChooser fc = new FileChooser();
				fc.setInitialDirectory(deskFile);

				//设置窗口名称
				fc.setTitle("打开文件");

				//添加过滤
				fc.getExtensionFilters().addAll(
						new ExtensionFilter("文本文件", "*.txt"),
						new ExtensionFilter("Java文件", "*.java")
						);

				Stage stage = new Stage();
				//File file= fc.showOpenDialog(stage);
				List<File> files= fc.showOpenMultipleDialog(stage);
				if(files ==null){
					return;
				}

				BufferedReader br = null;
				for(File f:files){
					try {
						br = new BufferedReader(new FileReader(f));
						String line=null;
						while((line=br.readLine()) !=null){
							ta.appendText(line);
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}finally{
						if(br !=null){
							try {
								br.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		});


		//打开文件夹
		b3.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Stage stage = new Stage();

				DirectoryChooser dc = new DirectoryChooser();
				dc.setTitle("打开文件夹");
				dc.setInitialDirectory(FileSystemView.getFileSystemView().getDefaultDirectory());
				File file = dc.showDialog(stage);
				if(file ==null){
					return;
				}
				System.out.println("目录绝对路径："+file.getAbsolutePath());
				File[]  list= file.listFiles();
				for(File f:list){
					ta.appendText(f.getAbsolutePath()+ "\r\n");
				}
			}
		});


		//保存文件
		b4.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Stage stage = new Stage();
				FileChooser fc = new FileChooser();
				fc.setInitialDirectory(FileSystemView.getFileSystemView().getHomeDirectory());
				fc.setInitialFileName("保存文件");
				fc.getExtensionFilters().add(
						new ExtensionFilter("文本文件", "*.txt")
						);
				File file=fc.showSaveDialog(stage);
				if(file ==null){
					return;
				}

				FileWriter fw= null;
				try {
					fw= new FileWriter(file);
					fw.write(ta.getText());
					fw.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					if(fw !=null){
						try {
							fw.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		});
	}

}
